import os
import os.path
import json
import sdfparse

__path__ = os.path.dirname(__file__)
# 数据目录
datafiles_path = os.path.join(__path__, '..' ,'data', 'sdf')
# 验证目录
goldenfiles_path = os.path.join(__path__, '..' ,'data' , 'sdf' , 'golden')

parsed_sdfs = list()
generated_sdfs = list()

def test_parse():
    files = sorted(os.listdir(datafiles_path))
    for f in files:
        if f.endswith('.sdf'):
            with open(os.path.join(datafiles_path, f)) as sdffile:
                parsed_sdfs.append(sdfparse.parse(sdffile.read()))

def test_emit():
    for s in parsed_sdfs:
        generated_sdfs.append(sdfparse.emit(s))


def test_output_stability():
    """ Checks if the generated SDF are identical with golden files"""

    parsed_sdfs_check = list()
    # read the golden files
    files = sorted(os.listdir(goldenfiles_path))
    for f in sorted(files):
        if f.endswith('.sdf'):
            with open(os.path.join(goldenfiles_path, f)) as sdffile:
                parsed_sdfs_check.append(sdffile.read())

    for s0, s1 in zip(parsed_sdfs, parsed_sdfs_check):
        sdf0 = sdfparse.emit(s0)
        assert sdf0 == s1


def test_parse_generated():
    for s in generated_sdfs:
        sdfparse.parse(s)


test_parse()

with open('./test_parse.json', 'w') as fp:
    json.dump(parsed_sdfs, fp, indent=4, sort_keys=True)

# test_emit()

# length = len(generated_sdfs)
# for index in range(length):
#     open('./test_emit_'+str(index)+'_.sdf', 'w').write(generated_sdfs[index])
    